var action;

jQuery(function() {

    jQuery('#commit_table_button').click(function() {
        
        
        if(jQuery('#table_id').val() == "" ){
            action = "TABLE_ADD";
        }	
        else{
            action = "TABLE_UPDATE";
            
        }
		
        if(jQuery('#table_name').val() == "")
        {
            alert('Table name field should not be empty');
            return false;
        }
        if(jQuery('#table_source').val()== "")
        {
            alert('Table source field should not be empty');
            return false;
        }
        if(jQuery('#table_physical_name').val() == "")
        {
            alert('Table physical name should not be empty');
            return false;
        }
        if(jQuery('#table_contents').val() == "")
        {
            alert('Table contents should not be empty');
            return false;
        }
        if(jQuery('#table_length').val() == "")
        {
            alert('Table length field should not be empty');
            return false;
        }
        if(jQuery('#table_description').val() == "")
        {
            alert('Table description field should not be empty');
            return false;
        }
        jQuery.ajax(
        {
            url : '?method=' + action,
            type : "POST",
            data :{
                'table_id'              : jQuery('#table_id').val(),
                'table_name'            : jQuery('#table_name').val(),
                'table_source'          : jQuery('#table_source').val(),
                'table_physical_name'   : jQuery('#table_physical_name').val(),
                'table_contents'        : jQuery('#table_contents').val(),
                'table_length'          : jQuery('#table_length').val(),
                'table_description'     : jQuery('#table_description').val()
            }
            , 
            dataType : "xml",
            success : function(xml) {
                alert(jQuery("message", xml).text());
                if(jQuery("success", xml).text() == "0") 
                {
                //fail
                }
                else
                {
                    var tableID = jQuery("tableID", xml).text();
                    if(action == "TABLE_ADD"){
                        jQuery('#table_id').val(tableID);
                    }
                    else if(action == "TABLE_UPDATE"){
                        //TODO
                    }
					
                    //var htmlResponse = jQuery("html", xml).text().replace("<![CDATA[", "").replace("]]>", "");
					
                    
                }
            }
                
        });
        
    });
	
});




